import type { App } from 'vue'
import { createI18n } from 'vue-i18n'
import Chinese from '@/locale/zh/index'
// #ifndef MP
import ZhTw from '@/locale/zh-tw/index'
import enUS from '@/locale/en/index'
// #endif
import {LOCALE} from '@/env'
import { setRequestHeader } from '@/common/request'
export type Locale = 'zh_CN' | 'zh_HK'| 'en_US';

const messages = {
	zh_CN: Chinese,
	// #ifndef MP
	zh_HK:ZhTw,
	en_US:enUS
	// #endif
}
// #ifndef MP 
uni.setStorageSync('locale', 'zh_CN');
// #endif
const i18n = createI18n({
	locale: uni.getStorageSync('locale') || LOCALE,
	messages
})
const init = (app:App) => {
	app.use(i18n)
}

let appLocale:Locale = uni.getStorageSync('locale') || LOCALE

export const getLocale = () => appLocale

export const setLocale = (locale:Locale) => {
	// #ifndef MP
	uni.setStorageSync('locale', locale)
	appLocale = locale
	let _locale:string = locale
	i18n.global.locale = locale
	if (locale === 'zh_CN') {
		_locale = 'zh-Hans'
	} else if (locale === 'zh_HK') {
		_locale = 'zh-Hant'
	}
	
	uni.setLocale(_locale)
	setRequestHeader()
	// #endif
}

export default init